<template>
	<view>
		<nav-bars title="手机号绑定"></nav-bars>
		<view class="BindPhone">
			<view class="BindPhone_nav">新手机号绑定</view>
			<view class="BindPhone_num"></view>
			<view class="BindPhone_Phone">
				<input :value="phoneValue" @input="bindKeyInput" type="number" maxlength="11" placeholder="请输入新手机号" placeholder-style="color:#999999;" />
			</view>
			<view class="BindPhone_code">
				<input type="text" v-model="VerificationValue" placeholder="请输入短信验证码" placeholder-style="color:#999999;" />
				<view class="BindPhone_send" v-if="disabled == false" @tap="GetVerificationCode">{{ sendTime }}</view>
				<view class="BindPhone_disabled" v-else>{{ sendTime }}</view>
			</view>
			<view class="BindPhone_btn"><view class="BindPhone_sure" @click="SureBindPhone">确认绑定</view></view>
		</view>
	</view>
</template>
<script>
import Https from '@/service/verification.js';
var Rexphone = /^1[2|3|4|5|6|7|8|9]\d{9}$/;
export default {
	data() {
		return {
			sendTime: '发送验证码', //倒计时
			currentTime: 60,
			disabled: false,
			phoneValue: '', //手机value
			VerificationValue: '' //验证码
		};
	},
	methods: {
		// 验证只能输入数字
		validateNumber(val) {
			return val.replace(/\D/g, '');
			// return val%100 !== 0
		},
		// 手机号码的双向数据绑定只能输入数字
		bindKeyInput(e) {
			var that = this;
			let value = that.validateNumber(e.detail.value);
			that.phoneValue = value;
			return value;
		},
		// 确认绑定
		SureBindPhone() {
			var that = this;
			var PhoneData = that.phoneValue;//获取手机号码
			var VerificationData = that.VerificationValue;//获取验证码
			if (PhoneData != '' && VerificationData != '' && Rexphone.test(PhoneData)) {
				let params = {
					phone: PhoneData,
					sms_code: VerificationData
				};
				Https.SetbindPhone(params).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '绑定成功',
							icon: 'success',
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 2
							});
						}, 2200);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			}
			if (PhoneData == '') {
				uni.showModal({
					title: '提示',
					content: '请输入手机号码！',
					showCancel: false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
						}
					}
				});
				return;
			} else if (!Rexphone.test(PhoneData)) {
				uni.showModal({
					title: '提示',
					content: '手机号码有误！',
					showCancel: false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
						}
					}
				});
				return;
			} else if (VerificationData == '') {
				uni.showModal({
					title: '提示',
					content: '请输入验证码！',
					showCancel: false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
						}
					}
				});
				return;
			}
		},
		// 获取验证码
		GetVerificationCode() {
			var that = this;
			var PhoneData = that.phoneValue;
			var VerificationData = that.VerificationValue;
			if (PhoneData == '') {
				uni.showModal({
					title: '提示',
					content: '请输入手机号码！',
					showCancel: false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
						}
					}
				});
				return;
			} else if (!Rexphone.test(PhoneData)) {
				uni.showModal({
					title: '提示',
					content: '手机号码有误！',
					showCancel: false,
					confirmText: '确定',
					success: function(res) {
						if (res.confirm) {
						}
					}
				});
				return;
			} else {
				if (that.disabled == false) {
					// that.GetCode();
					let params = {
						phone: that.phoneValue
					};
					Https.sendVerificationCode(params).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: '发送成功',
								icon: 'success',
								duration:2000
							});
							that.GetCode();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						}
					});
					// 调取API
				} else {
					uni.showModal({
						title: '提示',
						content: that.sendTime,
						showCancel: false,
						confirmText: '确定',
						success: function(res) {
							if (res.confirm) {
							}
						}
					});
				}
			}
		},
		// 获取验证码的时间倒计时
		GetCode: function(options) {
			var that = this;
			var currentTime = that.currentTime;
			var interval = setInterval(function() {
				currentTime--;
				that.disabled = true;
				that.sendTime = currentTime + '秒后重发';
				if (currentTime <= 0) {
					clearInterval(interval);
					that.sendTime = '重新发送';
					that.currentTime = 60;
					that.disabled = false;
				}
			}, 1000);
		}
	}
};
</script>
<style lang="less">
page {
	background: #fff;
}
.BindPhone {
	padding: 32rpx;
	.BindPhone_nav {
		font-size: 60rpx;
		font-weight: 500;
		color: #333333;
		margin-top: 140rpx;
		margin-bottom: 26rpx;
	}
	.BindPhone_num {
		font-size: 34rpx;
		font-weight: 500;
		color: #c3ab7c;
	}
	.BindPhone_Phone {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 200rpx;
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid #f1f1f1;
		input {
			flex: 1;
			margin-right: 30rpx;
			height: 100%;
			font-size: 30rpx;
		}
	}
	.BindPhone_code {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 40rpx;
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid #f1f1f1;
		input {
			flex: 1;
			margin-right: 30rpx;
			height: 100%;
			font-size: 30rpx;
		}
		.BindPhone_send {
			margin-right: 40rpx;
			font-size: 30rpx;
			font-weight: bold;
			color: #c3ab7c;
		}
		.BindPhone_disabled {
			margin-right: 40rpx;
			font-size: 30rpx;
			font-weight: bold;
			color: #999999;
		}
	}
	.BindPhone_btn {
		padding-top: 50rpx;
		.BindPhone_sure {
			font-size: 34rpx;
			font-weight: 500;
			color: #ffffff;
			height: 90rpx;
			background: #c3ab7c;
			border-radius: 10rpx;
			margin-bottom: 30rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.BindPhone_Change {
			height: 90rpx;
			border: 2rpx solid #c3ab7c;
			border-radius: 10rpx;
			font-size: 34rpx;
			font-weight: 500;
			color: #c3ab7c;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
}
</style>
